import java.util.Arrays;

/**
 * @author gao
 * @date 2025/11/6 23:57
 */

public class Solution {
    public void duplicateZeros(int[] arr) {
        int n = arr.length, cur = 0, dest = -1;

        while (cur < n) {
            if (arr[cur] == 0) {
                dest += 2;
            } else {
                dest++;
            }

            if (dest >= n - 1) {
                break;
            }
            cur++;
        }

        if (dest == n) {
            arr[n - 1] = 0;
            dest -= 2;
            cur--;
        }

        while (cur >= 0) {
            if (arr[cur] == 0) {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            } else {
                arr[dest--] = arr[cur--];
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 0, 2, 3, 0, 4, 5, 0};
        Solution solution = new Solution();
        solution.duplicateZeros(arr);
        System.out.println(Arrays.toString(arr));
    }
}